package twitter.client;

import org.apache.commons.lang3.StringUtils;

import twitter.client.listener.TweetTextOnEditorActionListener;
import twitter.client.util.Global;
import twitter.client.util.Utils;
import twitter4j.TwitterException;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class TweetActivity extends Activity {

	private EditText postText;
	private TextView tweetPostTextLength;
	private Button tweetExecButton;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tweet);

		Log.d("TweetActivity", "onCreate");

		postText = (EditText) findViewById(R.id.tweetPostText);
		postText.setOnEditorActionListener(new TweetTextOnEditorActionListener());

		Handler handler = new Handler();
		handler.postDelayed(new Runnable() {

			public void run() {
				Log.d("postDelayed", "run");
				InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
				imm.showSoftInput(postText, 0);
			}
		}, 300);

		// postText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
		//
		// public void onFocusChange(View v, boolean hasFocus) {
		// Log.d("onFocusChange", ""+hasFocus);
		// InputMethodManager imm = (InputMethodManager)
		// getSystemService(Context.INPUT_METHOD_SERVICE);
		// if(hasFocus){
		// imm.showSoftInput(v, InputMethodManager.SHOW_FORCED);
		// }else{
		// imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
		// }
		//
		// }
		// });

		// postText.setFocusable(true);
		// postText.requestFocus();
		// postText.addTextChangedListener(this);
		tweetPostTextLength = (TextView) findViewById(R.id.tweetPostTextLength);
		tweetExecButton = (Button) findViewById(R.id.tweetExecButton);

		// LinearLayout layout = (LinearLayout)
		// findViewById(R.id.tweetPanelView);
		//
		// if (layout.getVisibility() == View.INVISIBLE) {
		// layout.setVisibility(View.VISIBLE);
		// postText.requestFocus();
		// InputMethodManager imm = (InputMethodManager)
		// getSystemService(Context.INPUT_METHOD_SERVICE);
		// imm.showSoftInput(postText, InputMethodManager.SHOW_IMPLICIT);
		// imm.showSoftInput(postText, 0);
		// } else {
		// layout.setVisibility(View.INVISIBLE);
		// // imm.hideSoftInputFromWindow(postText.getWindowToken(), 0);
		// }
	}

	@Override
	protected void onStart() {
		super.onStart();
		// InputMethodManager imm = (InputMethodManager)
		// getSystemService(Context.INPUT_METHOD_SERVICE);
		// imm.showSoftInput(postText, InputMethodManager.SHOW_IMPLICIT);
	}

	public void tweetExecButtonOnClick(View v) {
		String text = this.postText.getText().toString();
		if (StringUtils.isEmpty(text)) {
			return;
		}

		boolean isSuccess = true;

		try {
			Global.currentTwitter.updateStatus(text);
		} catch (TwitterException e) {
			Utils.showToast(this, "ツイートに失敗しました。");
			isSuccess = false;
		}

		if (isSuccess) {
			finish();
		}
	}

}
